//
//  TBCReadingListView.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCReadingListView.h"
#import "TBCReadingList.h"
#import "TBCReadingListCell.h"

@interface TBCReadingListView () <TBCReadingList, UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *arrayList;
@property (strong, nonatomic) BaseTableView *tableViewReadingList;

@end

@implementation TBCReadingListView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initTBCReadingListData];
        
    }
    
    return self;
    
}

- (void)initTBCReadingListViews {
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.tableViewReadingList = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableViewReadingList.delegate = self;
    self.tableViewReadingList.dataSource = self;
    [self addSubview:self.tableViewReadingList];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getHeaderData)];
    self.tableViewReadingList.header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getFooterData)];
    self.tableViewReadingList.footer = footer;
    
}

- (void)initTBCReadingListData {
    
    [TBCReadingList shareReadingList].delegate = self;
    
}

- (void)sendAllArray:(NSMutableArray *)arrayList {
    
    self.arrayList = [NSMutableArray arrayWithCapacity:0];
    
    self.arrayList = arrayList;
    
    [self initTBCReadingListViews];
    
}

#pragma mark -- 设置下拉刷新
- (void)getHeaderData {
    
    [self.tableViewReadingList.header beginRefreshing];
    [self.delegate refreshData];
    [self.tableViewReadingList.header endRefreshing];
    
}

#pragma mark -- 设置上拉加载
- (void)getFooterData {
    
    [self.delegate getMoreData];
    [self initTBCReadingListData];
    [self.tableViewReadingList reloadData];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return (HEIGHT - 64) * 0.25;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrayList.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"readingList";
    
    TBCReadingListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        
        cell = [[TBCReadingListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    TBCReadingList *reading = [self.arrayList objectAtIndex:indexPath.row];
    
    cell.reading = reading;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    TBCReadingList *reading = [self.arrayList objectAtIndex:indexPath.row];
    
    [self.delegate pushToController:reading.detail_id];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
